package com.zhku.user.controller;


import com.zhku.base.model.PageParams;
import com.zhku.base.model.PageResult;
import com.zhku.base.model.RestResponse;
import com.zhku.user.model.dto.AddressDto;
import com.zhku.user.model.dto.QueryAddressParamsDto;
import com.zhku.user.model.po.TtUserAddress;
import com.zhku.user.service.TtUserAddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@Slf4j
@Api(value = "用户地址相关接口",tags = "用户地址相关接口")
@RestController
public class TtUserAddressController {

    @Autowired
    private TtUserAddressService ttUserAddressService;

    @ApiOperation("用户创建新收货地址")
    @PostMapping("/createAddress")
    public RestResponse<TtUserAddress> createAddress(@RequestBody AddressDto addressDto){
        TtUserAddress ttUserAddress = ttUserAddressService.createAddress(addressDto);
        return RestResponse.success(ttUserAddress);
    }

    @ApiOperation("用户删除收货地址")
    @PostMapping("/deleteAddress/{id}")
    public RestResponse deleteAddress(@PathVariable("id") String id){
        ttUserAddressService.deleteAddress(id);
        return RestResponse.success();
    }

    @ApiOperation("用户修改收货地址")
    @PostMapping("/updateAddress")
    public RestResponse<TtUserAddress> updateAddress(@RequestBody TtUserAddress ttUserAddress){
        TtUserAddress ttUserAddressNew = ttUserAddressService.updateAddress(ttUserAddress);
        return RestResponse.success(ttUserAddressNew);
    }

    @ApiOperation("查询收货地址")
    @GetMapping("/address/{id}")
    public RestResponse<TtUserAddress> getAddress(@PathVariable("id") String id){
        TtUserAddress ttUserAddress = ttUserAddressService.getAddress(id);
        return RestResponse.success(ttUserAddress);
    }

    @ApiOperation("查询当前用户的所有收货地址")
    @PostMapping("/address/list")
    public PageResult<TtUserAddress> list(PageParams pageParams, @RequestBody(required = false) QueryAddressParamsDto queryAddressParamsDto){
        PageResult<TtUserAddress> addressPageResult = ttUserAddressService.queryAddressList(pageParams,queryAddressParamsDto);
        return addressPageResult;
    }
}
